package com.lunwen.lunwen.api;

import com.lunwen.lunwen.bean.*;
import com.lunwen.lunwen.config.ShiroUtils;
import com.lunwen.lunwen.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Mubantask)表控制层
 *
 * @author makejava
 * @since 2020-08-05 17:17:59
 */
@RestController
@RequestMapping("api")
public class MubantaskController {

    /**
     * 服务对象
     */
    @Autowired
    private MubantaskService mubantaskService;

    @Autowired
    private XueShengService xueShengService;

    @Autowired
    private DaoShiService daoShiService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private MuBanService muBanService;

    @Autowired
    private LunWenXinXiService lunWenXinXiService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("mubantaskselectOne")
    public Result mubantaskselectOne(Integer id) {
        Result result = new Result();
        Mubantask mubantask = this.mubantaskService.queryById(id);
        result.setResult("200","查询成功",mubantask);
        return result;
    }

    /**
     * 列出对应模板和学生的文档参数数据。
     *
     * @param
     * @return 数据
     */
    @RequestMapping("mubantasklist")
    public Result mubanTasklist(@RequestBody Mubantask t)
    {
        Result result = new Result();
        if (StringUtils.isEmpty(t.getXueshengid())){
            result.setResult("201","学生ID不能为空！");
            return result;
        }
        if (StringUtils.isEmpty(t.getMubanid())){
            result.setResult("201","模板ID不能为空！");
            return result;
        }

        XueSheng xueSheng = xueShengService.queryById(t.getXueshengid());
        if (xueSheng == null){
            result.setResult("201","不存在该学生！");
            return result;
        }

        MuBan muBan = muBanService.queryById(t.getMubanid());
        if (muBan == null){
            result.setResult("201","不存在该模板！");
            return result;
        }

        System.out.println(xueSheng.getId());
        List<Mubantask> mubantasks = mubantaskService.queryAllBy(t);
        result.setResult("200","查询成功！",mubantasks);
        return result;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @RequestMapping("mubantaskinsert")
    public Result mubantaskinsert(@RequestBody Mubantask t)
    {
        Result result = new Result();
        if (StringUtils.isEmpty(t.getXueshengid())){
            result.setResult("201","学生ID不能为空！");
            return result;
        }
        if (StringUtils.isEmpty(t.getMubanid())){
            result.setResult("201","模板ID不能为空！");
            return result;
        }
        XueSheng xueSheng = xueShengService.queryById(t.getXueshengid());
        if (xueSheng == null){
            result.setResult("201","不存在该学生！");
            return result;
        }
        MuBan muBan = muBanService.queryById(t.getMubanid());
        if (muBan == null){
            result.setResult("201","不存在该模板！");
            return result;
        }

        LunWenXinXi lunWenXinXi = new LunWenXinXi();
        lunWenXinXi.setXueshengId(xueSheng.getUserId());
        List<LunWenXinXi> lunWenXinXis = lunWenXinXiService.queryAllByLimit(lunWenXinXi, 0, 1);
        LunWenXinXi lunWenXinXi1 = lunWenXinXis.get(0);
        System.out.println(lunWenXinXi1);
        if (lunWenXinXi1 == null){
            result.setResult("201","不存在与该用户绑定的论文信息！");
            return result;
        }
        Mubantask insert=null;
        Mubantask check=this.mubantaskService.checkBy(t);

        if(check == null||check.getMubanid()==null) {
            System.out.println("kong");
             insert = this.mubantaskService.insert(t);
        }
        else {
             this.mubantaskService.update(t);
        }
        result.setResult("200","插入成功",insert);
        return result;
    }

    /**
     * 返回登录学生的论文基本信息
     * @return
     */
    @RequestMapping("thesisbaseinfo")
    public Result thesisbaseinfo(){


        Thesisbaseinfo thesisbaseinfo=new Thesisbaseinfo();

        Result result = new Result();
        if (ShiroUtils.getUserEntity() == null){
            result.setResult("201","用户未登录！");
            return result;
        }
        XueSheng xueSheng = xueShengService.queryByUserId(ShiroUtils.getUserEntity().getUserId());
        if (xueSheng == null){
            result.setResult("201","不存在该学生！");
            return result;
        }


       // ReturnThesisbaseinfo returnThesisbaseinfo = new ReturnThesisbaseinfo();
        LunWenXinXi lunWenXinXi = new LunWenXinXi();

        lunWenXinXi.setXueshengId(xueSheng.getUserId());
        List<LunWenXinXi> lunWenXinXis = lunWenXinXiService.queryAllByLimit(lunWenXinXi, 0, 1);
        LunWenXinXi lunWenXinXi1 = lunWenXinXis.get(0);
        if (lunWenXinXi1 == null){
            result.setResult("201","不存在与该用户绑定的论文信息！");
            return result;
        }


        thesisbaseinfo.setThesistitle(lunWenXinXi1.getLunwentimu());
        thesisbaseinfo.setThesissourse(lunWenXinXi1.getTimulaiyuan());
        thesisbaseinfo.setThesistype(lunWenXinXi1.getTimuleixing());
        SysUser sysUser = sysUserService.queryById(lunWenXinXi1.getXueshengId());
        thesisbaseinfo.setStudentnumber(sysUser.getUsername());
        thesisbaseinfo.setStudentname(sysUser.getTruename());



        DaoShi daoShi = daoShiService.queryByUserId(lunWenXinXi1.getDaoshiId());

        thesisbaseinfo.setSupervisorname(daoShi.getSysUser().getTruename());
        thesisbaseinfo.setZhicheng(daoShi.getZhicheng());


        /***
         * 以下内容需要优化，动态查询。
         */
        thesisbaseinfo.setCollege("计算机学院");
        thesisbaseinfo.setDepartment("计算机科学与技术");
        thesisbaseinfo.setClasses("2017级1班");

        result.setResult("200","查询成功！");
        result.setData(thesisbaseinfo);
        return result;
    }

}